package com.hmall.order;

import com.hmall.feign.FeignClient;
import com.hmall.order.mapper.OrderDetailsMapper;
import com.hmall.order.mapper.OrderMapper;

import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

import java.util.Date;
import java.util.List;

@Component
public class TestTask {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Autowired
    private FeignClient feignClient;

    //               秒  分  时 天 月 周
    @Scheduled(cron = "0 */1 * * * ?") // 每隔1分钟
    public void handleTast() {
        Integer status = 1;

//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳

        Date date = new Date();

        List<Order> orderList = orderMapper.getOrderByState(status);
        for (Order order : orderList) {
            Date createTime = order.getCreateTime();
            if (date.getTime() - createTime.getTime() > 1 * 60 * 1000) {
                orderMapper.updateOrderStateByOrderId(5,order.getId());
                System.out.println(order.getId()+11);
                OrderDetail orderDetail = orderDetailsMapper.findOrderDetailsById(order.getId());
                System.out.println(orderDetail);
                Integer num = orderDetail.getNum();
                Long itemId = orderDetail.getItemId();
                feignClient.reliveStock(itemId, num);
            }


        }

        System.out.println(date);

        System.out.println("每隔1分钟执行一次 ==>" + LocalDateTime.now());
    }
    //               秒  分  时 天 月 周
//    @Scheduled(cron = "*/1 * * * * ?") // 每隔1秒钟
//    public void handleTast2(){
//        System.out.println("每隔1秒执行一次 ==>" + LocalDateTime.now());
//    }


}